/**
 * SpringMVC restful 风格
 * 发票管理控制器，控制关于商品的一些业务逻辑操作
 * @author zhangq
 * @date 2015-03-09
 */
package com.chnbst.bstyun.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Invoice;
import com.chnbst.bstyun.model.pojo.InvoiceModel;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.AddressVo;
import com.chnbst.bstyun.model.vo.InvoiceVo;
import com.chnbst.bstyun.service.AddressService;
import com.chnbst.bstyun.service.InvoiceModelService;
import com.chnbst.bstyun.service.InvoiceService;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/finance/invoice")
public class InvoiceController {
	
	/**
	 * 注入业务逻辑层
	 */
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private InvoiceModelService invoiceModelService;
	@Autowired
	private AddressService addressService;	
	@Autowired
	private UserService userService;
	
	/**
	 * @return 发票管理  根据条件查询发票列表  /finance/invoice/invoiceList
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/invoiceList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> invoiceList(HttpServletRequest request) throws Exception {
		User user = (User)request.getSession().getAttribute("userInfo");
		Map<String, Object> map = new HashMap<String, Object>();
		
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String invoiceState = request.getParameter("invoiceState");
		if(SignUtil.isNotEmpty(startTime)) {
			startTime += " 00:00:00";
		}
		if(SignUtil.isNotEmpty(endTime)) {
			endTime += " 23:59:59";
		}
		
		map.put("userId",user.getUserId());
		map.put("lastDate", startTime);
		map.put("nowDate", endTime);
		map.put("invoiceState", invoiceState);
		
		List<InvoiceVo> invoicesList = invoiceService.selectInvoiceList(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("invoicesList", invoicesList);
		return resultMap;
	}
	
	/**
	 * @return 发票管理  修改发票信息  /finance/invoice/updateInvoice
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/updateInvoice", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateInvoice(Invoice invoice,HttpServletRequest request) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		
		isSuccess = invoiceService.updateInvoice(invoice);
		
		map.put("isSuccess", isSuccess);
		return map;
	}
	
	/**
	 * @return 发票管理  根据条件查询单条数据详情  /finance/invoice/invoiceDetail
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/invoiceDetail/{invoiceId}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> invoiceDetail(@PathVariable Integer invoiceId,HttpServletRequest request){
		InvoiceVo invoice = invoiceService.selectInvoiceVo(invoiceId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("invoice", invoice);
		return map;
	}
	
	/**
	 * @return 索取发票 主页面  /finance/invoice/invoiceDemand
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/invoiceDemand", method = RequestMethod.GET)
	public ModelAndView invoiceDemand(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("users/finance_manage/invoice_manage/invoiceDemand");
		return mav;
	}
	
	/**
	 * @return 索取发票详情 页面  /finance/invoice/invoiceDemandDetail
	 * @throws Exception  
	 */ 
	@RequestMapping(value = "/invoiceDemandDetail", method = RequestMethod.GET)
	public ModelAndView invoiceDemandDetail(HttpServletRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		User userInfo = (User)request.getSession().getAttribute("userInfo");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userInfo.getUserId());
		InvoiceModel invoiceModel = invoiceModelService.selectByCondition(map);
		List<AddressVo> addressVos = addressService.selectAddressVos(userInfo.getUserId());
		mav.addObject("addressVos", addressVos);
		mav.setViewName("users/finance_manage/invoice_manage/invoiceDemandDetail");
		mav.addObject("userInfo", userInfo);
		mav.addObject("invoiceModel", invoiceModel);
		
		return mav;
	}
	
	/**
	 * 新增发票模板信息
	 * @return 发票模板信息
	 * @throws Exception
	 */
	@RequestMapping(value="/addInvoiceModel",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addInvoiceModel(InvoiceModel invoiceModel,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		boolean isSuccess = false;
		//新增
		invoiceModel.setUserId(user.getUserId());
		invoiceModel.setAddTime(new Date());
		isSuccess = invoiceModelService.insertInvoiceModel(invoiceModel);
		
		map.put("isSuccess", isSuccess);
		return map;
	}
	
	/**
	 * 新增发票信息
	 * @return 发票信息列表
	 * @throws Exception
	 */
	@RequestMapping(value="/addInvoice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addInvoice(Invoice invoice,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		//获取用户的发票模板
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("userId", user.getUserId());
		InvoiceModel invoiceModel = invoiceModelService.selectByCondition(map1);
		boolean isSuccess ,b;
		//新增
		invoice.setUserId(user.getUserId());
		invoice.setAddTime(new Date());
		invoice.setInvoiceState("1");
		invoice.setInvoiceModelId(invoiceModel.getInvoiceModelId());
		
		b = invoiceService.insertInvoice(invoice);
		
		//修改用户的可索取发票金额
		if(b){
			user.setInvoiceMoney(user.getInvoiceMoney()-invoice.getInvoiceMoney());
			isSuccess = userService.updateUser(user);
			
			map.put("isSuccess", isSuccess);
		}
		return map;
	}
}